<template>
<div class="login_container">
  <div class="login_box">
    <el-form :model="loginForm" ref="loginFormRef" :rules="loginRules" class="el-form" label-width="0px">
  <el-form-item prop="username">
    <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid"></el-input>
  </el-form-item>
  <el-form-item prop="password">
    <el-input type="password" v-model="loginForm.password" prefix-icon="el-icon-more"></el-input>
  </el-form-item>
  <el-form-item class="btn">
      <el-button type="primary" @click="login">登录</el-button>
      <el-button type="info" @click="resetLoginForm">重置</el-button>
  </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 6, max: 13, message: '长度在 6 到 13 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data } = await this.$http.post('login', this.loginForm)
        console.log(data)
        console.log(data.token)
        if (data.meta.status !== 200) return this.$message.error('登录失败')
        this.$message.success('登录成功')
        window.sessionStorage.setItem('token', data.data.token)
        this.$router.push('/home')
      })
    }
  }

}
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: skyblue;
  .login_box{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
    border-radius: 3px;
    width: 450px;
    height: 300px;
    background-color: #fff;
  }
  .el-form{
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
  .btn{
    display: flex;
    justify-content: flex-end;
  }
}

</style>
